package com.lhr.common.search;

import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import com.lhr.common.domain.AbstractBean;
import com.lhr.common.util.ValidateUtil;


public class LuceneSettings extends AbstractBean {

	private static final long	serialVersionUID	= 1L;
	private Analyzer			analyzer;
	private Directory			directory;
	private Version				version				= Version.LUCENE_31;
	private OpenMode			openMode;

	public LuceneSettings(boolean isUseRAMDirectory, String... indexDirectory) throws CorruptIndexException, LockObtainFailedException, IOException {
		this.analyzer = new IKAnalyzer(); // {@link LimitTokenCountAnalyzer}
		if (isUseRAMDirectory)
			this.directory = new RAMDirectory();
		else {
			if (ValidateUtil.isEmpty(indexDirectory))
				throw new NullPointerException("if isUseRAMDirectory is false, indexDirectory can not be empty");
			File indexFile = new File(indexDirectory[0]);
			indexFile.mkdirs();
			this.directory = FSDirectory.open(indexFile);
		}
	}

	public Analyzer getAnalyzer() {
		return analyzer;
	}

	public void setAnalyzer(Analyzer analyzer) {
		this.analyzer = analyzer;
	}

	public Directory getDirectory() {
		return directory;
	}

	public void setDirectory(Directory directory) {
		this.directory = directory;
	}

	public Version getVersion() {
		return version;
	}

	public void setVersion(Version version) {
		this.version = version;
	}

	public OpenMode getOpenMode() {
		return openMode;
	}

	public void setOpenMode(OpenMode openMode) {
		this.openMode = openMode;
	}
}
